package cn.xz.zysjz.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.Objects;

import cn.xz.zysjz.R;
import cn.xz.zysjz.adapter.ActivityListAdapter;
import cn.xz.zysjz.bean.AppData;
import cn.xz.zysjz.bean.CommonLib;
import cn.xz.zysjz.bean.HomeListBean;
import cn.xz.zysjz.databinding.ActivityLikeBinding;
import cn.xz.zysjz.utils.AppHelper;
import cn.xz.zysjz.utils.SessionUtils;
import cn.xz.zysjz.utils.ToastUtils;

/**
 * 收藏列表*/
public class LikeActivity extends AppCompatActivity {

    ActivityLikeBinding mBinding;

    private ActivityListAdapter mAdapter; //列表适配器


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_like);
        if (Build.VERSION.SDK_INT >= 21) {
            this.getWindow()
                    .setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
        initEvent();
        //列表设置
        initRV();
    }

    private void initRV(){
        //列表相关数据
        LinearLayoutManager ms = new LinearLayoutManager(this);
        mAdapter = new ActivityListAdapter(R.layout.item_activity_list);
        mBinding.rvList.setLayoutManager(ms);
        mBinding.rvList.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                ArrayList<HomeListBean> data = (ArrayList<HomeListBean>) adapter.getData();
                //跳转到详情页
                AppHelper.toJump(IndexActivity.class,data.get(position));
            }
        });


    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mAdapter != null){
            //获取收藏列表
            ArrayList<HomeListBean> list = SessionUtils.getList(CommonLib.SC_LIST, HomeListBean.class);
            ArrayList<HomeListBean> mList  = new ArrayList<>();
            for (HomeListBean homeListBean: list){
                if (Objects.equals(homeListBean.getScUser(), CommonLib.nowUserBean.getName())){
                    mList.add(homeListBean);
                }
            }
            mAdapter.setList(mList);
        }
    }

    private void initEvent(){
        //返回
        mBinding.viewBack.setOnClickListener(v->{
            finish();
        });
    }
}